package com.zwan.oam_rtc.imps;

import com.zwan.oam_rtc.utils.messages.MessageHead;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.MessageFormat;


@Slf4j
public class HandlerRunner implements Runnable{
	private MessageHead head;
	private ByteBuffer bf;
	private ByteOrder byteOrder;

	public HandlerRunner(MessageHead head, ByteBuffer bf, ByteOrder byteOrder){
		this.head = head;
		this.bf = bf;
		this.byteOrder = byteOrder;
	}

	public synchronized void run(){
		//根据客户端程序号Pno选择在handlers中选择Pnohandler（其实是它的子类），选择与程序对应的handler（这儿只有UserStatusHandler），其实就看配置文件了
		PnoHandler handler = JavaImp.getInstance().getHandler(head.getDestPno());
		//测试可用instance判断一下
		if (handler != null){
			//第三个参数为ByteBuffer，则调用basicHandler中的sercive方法
			handler.service(head, head.getEventid(), bf, byteOrder);
		}
		else{
			log.info(MessageFormat.format("Recive a msg to pno: {0}, but no Handler match", head.getDestPno()));
		}
	}
}
